<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Seat as BusObj;
use app\common\business\SeatDetail;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

class Seat extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function index()
    {
        $this->indexField = "*,venue as get_venue,theatre_cate_id as get_theatre_cate_id";
        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function dialog()
    {
        $id = $this->request->param('id', 0, 'intval');
//        dump($id);exit();
        //根据id查询座位
        try {
            $detail = (new SeatDetail())->getSeatDetailByList($id);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        $seatDetail = Show::getArraySeat($detail);
        $lists = Show::getArrayList($detail);
        return $this->fetch('', [
            "id" => $id,
            "seatDetail" => $seatDetail,
            "all" => Show::json_encode($lists['all'] ?? []),
//            "dis" => Show::json_encode($lists['dis'] ?? []),
        ]);
    }

    public function submit()
    {
        $all = $this->request->param('all', '', 'trim');
        $disabled = $this->request->param('disabled', '', 'trim');
        $seat_id = $this->request->param('seat_id', 0, 'intval');
        if ($seat_id == 0) {
            return Show::json_show(Status::status_error, '请关闭重试');
        }
        $data = [
            "all" => $all,
            "disabled" => $disabled
        ];
        try {
            $result = (new SeatDetail())->addSeatDetail($data, $seat_id);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "保存成功");
        } else {
            return Show::json_show(Status::status_error, "保存失败");
        }
    }

    public function getSeatByList()
    {
        $cate_id = $this->request->param('cate_id',0,'intval');
        if($cate_id == 0){
            return Show::json_show(Status::status_error,"内部错误");
        }
        try {
            $lists = $this->busObj->getSeatByList($cate_id,"id as value,title as name");
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        return Show::json_show(Status::status_normal, "ok", $lists);
    }

}